package com.elibrary;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import nimo.elibrary.actions.User;
import nimo.elibrary.db.model.UserDataSource;

/**
 * Created with IntelliJ IDEA.
 * User: Enkuso
 * Date: 11/29/12
 * Time: 4:45 PM
 * To change this template use File | Settings | File Templates.
 */
public class Signup extends BaseActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        //requestWindowFeature(Window.FEATURE_NO_TITLE);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(R.layout.signup);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }

    public void Signup(View btn)
    {

        EditText username = (EditText) findViewById(R.id.etUsername);
        EditText email = (EditText) findViewById(R.id.etEmailAddress);
        EditText password = (EditText) findViewById(R.id.etPassword);
        TextView tvError = (TextView) findViewById(R.id.tvError);

        nimo.elibrary.json.model.user.User signedUser = User.Signup(username.getText().toString(), email.getText().toString(), password.getText().toString());
        if(signedUser != null){
            // Амжилттай бүртгүүлсэн талаар мэдээлэх
            Toast.makeText(this, R.string.signup_success, Toast.LENGTH_SHORT).show();
            // Нэвтэрсэн хэрэглэгчийн мэдээлэл утсанд хадгалах
            userDataSource.open();
            userDataSource.createOrUpdateUser(username.getText().toString(), null, password.getText().toString(),signedUser.token,null,null);
            userDataSource.close();

            app.setLoggedIn(signedUser.token);

            setSignoutMenuBtn();

            finish();
        } else {
            tvError.setText(R.string.signup_error);
        }
    }
}
